package com.axend.lib_base.view.dialog

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.view.WindowManager
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.axend.lib_base.R
import com.axend.lib_base.databinding.BaseDialogCustomWarningBinding
import com.axend.lib_base.ext.dp2px
import com.axend.lib_base.utils.AudioPlayer

/**
 * 自定义警告弹窗
 */

class CustomWarningDialog(
    context: Context,
) : Dialog(context, R.style.Common_CustomDialog) {

    private var dialogBinding: BaseDialogCustomWarningBinding
    private val blinkAnimation by lazy {
        createBlinkAnimation()
    }
    private var warningType: String = ""

    /*是否手动取消 ture:手动取消 false:6秒自动取消*/
    private var isManualMode: Boolean = false

    companion object {
        @Volatile
        private var instance: CustomWarningDialog? = null

        fun getInstance(context: Context): CustomWarningDialog {
            return instance ?: synchronized(this) {
                instance ?: createInstance(context).also { instance = it }
            }
        }

        private fun createInstance(context: Context): CustomWarningDialog {
            return CustomWarningDialog(context).apply {
                // 初始化Dialog的配置，比如设置视图、按钮监听器等
                setCancelable(true)
                // 这里可以根据需要设置对话框的内容，例如使用自定义布局
            }
        }
    }


    init {
        dialogBinding =
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.base_dialog_custom_warning,
                null,
                false
            )
        setContentView(dialogBinding.root)
        setCanceledOnTouchOutside(false)
        val window = window
        window?.apply {
            val layoutParams = window.attributes
            layoutParams.dimAmount = 0.5f // 设置透明度
            window.attributes = layoutParams
            clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        }
        //设置提示框位置
        val lp = window!!.attributes
        //lp.gravity = Gravity.TOP | Gravity.LEFT;
        //lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
        //lp.x = DisplayUtils.dip2px(53);
        //lp.y = DisplayUtils.dip2px(186);
//        lp.width = dp2px(540f)
        window.attributes = lp

        dialogBinding.ivClose.setOnClickListener {
            dismiss()
            AudioPlayer.getInstance(context).stop()
        }
        dialogBinding.btOk.setOnClickListener {
            dismiss()
            AudioPlayer.getInstance(context).stop()
        }

    }

    /**
     * 显示弹窗
     */
    fun show(tips: String, warningType: String = ""): Dialog {
        dialogBinding.tvWarning.text = tips
        blinkAnimation.start()
        AudioPlayer.getInstance(context)
            .play(R.raw.warning)
        this.warningType = warningType
        this.show()
        return this@CustomWarningDialog
    }

    /**
     * 设置是否手动取消
     */
    fun setIsManualMode(isManualMode: Boolean) {
        this.isManualMode = isManualMode
    }


    private fun createBlinkAnimation(): ObjectAnimator {
        return ObjectAnimator.ofFloat(
            dialogBinding.ivWarning,
            View.ALPHA,
            0f, 1f
        ).apply {
            if (isManualMode) {
                repeatCount = ValueAnimator.INFINITE
                repeatMode = ValueAnimator.REVERSE
            } else {
                duration = 500
                repeatCount = 12
            }
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    dismiss()
                }

            })
        }
    }


    override fun dismiss() {
        if (this.isShowing) AudioPlayer.getInstance(context).stop()
        super.dismiss()
        if (blinkAnimation.isRunning) {
            blinkAnimation.cancel()
        }
    }


    fun getWarningType(): String {
        return warningType
    }


}